/**
 * chart panel
 */
package edu.gatech.oad.doctorsorder;


import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import edu.gatech.oad.records.TreatmentRecord;
import edu.gatech.oad.user.Patient;
/**
 * User interface to display charts
 * 
 * @author chen
 * @version 1.0
 */
public class ChartPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Create the panel.
	 * 
	 * @param usr
	 *            the patient the graph is describing
	 * 
	 */
	public ChartPanel(Patient usr) {
		// set variables
		final String type = "Weight";
		final List<TreatmentRecord> records = usr.getPatientHistory()
				.getTreatmentRecords();
		final String seriesName = type.equals("weigth") ? "Weight" : "Height";
		final String title = type.equals("weigth") ? "Weight Graph"
				: "Height Graph";
		final String xAxisName = "Time";
		final String yAxisName = type.equals("weigth") ? "Weight" : "Height";

		// set chart's data set
		final XYSeries dataset = new XYSeries(seriesName);
		int i = 1;

		for (TreatmentRecord x : records) {
			try {
				dataset.add((double) i, Double.parseDouble(type
						.equals("weigth") ? x.getWeight() : x.getHeight()));

				i++;
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null, "No history avaiable");
				Logger.getAnonymousLogger().log(Level.WARNING,
						e.getLocalizedMessage() + "no history data");

			}
		}

		// draw the chart
		final JFreeChart chart = ChartFactory.createScatterPlot(title,
				xAxisName, yAxisName, new XYSeriesCollection(dataset),
				PlotOrientation.VERTICAL, true, true, false);
		final org.jfree.chart.ChartPanel panel = new org.jfree.chart.ChartPanel(
				chart);
		add(panel);

	}

}
